;Draws the tilemap.



#macro DrawHL(y, x)

 
 push de
 push bc
 push af
 push ix
 push hl
 LD hl,x*256+y
 LD (curRow),hl
 pop hl
 push hl
 B_CALL _DispHL
 B_CALL _getKey
 pop hl
 pop ix
 pop af
 pop bc
 pop de
 
#endmacro
	
Tile_width = 2
Map_Entry_Size = 1
MAP_MASK = $ff
PMAPWIDTH = 6
PMAPHEIGHT = 5
PMAPHIEGHT = PMAPHEIGHT
delta = savesscreen


Start:
	di

	;Timing in the game, including "forcing" FPS depending
	;on what speed we want, is done via interrupts.
	;This is to achieve a consistant speed.
	;So we need to load interrupt information.

	ld hl, Play_One_SAD_Frame
	ld de, $9A9A
	ld bc, End_SAD_Frame - Play_One_SAD_Frame
	ldir

	ld a, $99
	ld i, a



    LD     HL, $9900
    LD     DE, $9901
    LD     BC, 256
	ld A, $9a
    LD     ($9900), A
    LDIR

;This is where the game is loaded.  Thus this code is run only once.

	xor a			;Start with 0 buildings.
	ld (BuildingCount), a


;The game requires user RAM.

	ld hl,21000
	ld de,$9d95
	bcall(_InsertMem)

;Savescreen is used for saving data involving maps.  Thus, we need to disable
;the APD so that the data will not be corrupted.


	RES apdAble,(IY+apdFlags)

;Structure_Locations holds the locations of all buildings that could possibly exit,
;up to 200.  Four bytes for each building: TileX, TileY, 6 bits for the building's
;ID, and 10 bits for the buildings HP.

Structure_Locations .equ $9D95 + 13500
Ship_Frame_Data .equ $9D95 + 15500

;The Map Data, containing the tiles in tilemap form.  Maps can be no bigger than
;3600 bytes, but a 3600 byte map requires 900 bytes for fog-of-war and collision
;information.  Therefore, 4500 bytes is needed in this area.

MapData .equ $9D95 + 5000

;Tile Data.  Requires 8000 bytes.

TileData .equ $9D95 + 9500
	

;To optimize the drawing of structures

Store_Structure_Sprite_Addresses:

;	B_CALL _Load_Sprite_Data
	B_CALL _Load_Map_Data
	B_CALL _Load_Tile_Data


	


	
	
	ld hl,2
	ld (delta),hl
	ld (DeltaPreserve), hl
	ld a,1
	ld (Num_Layers),a
	xor a
	ld h,a
	ld a,(mapdata)
	ld l,a
	ld (mapheight),hl
	ld a,(mapdata+1)
	ld l,a
	ld (Mapwidth),hl
	ld hl,0
	ld (MapY),hl
	ld (MapX),hl
	ld hl,TileData
	ld (Tileptr),hl
	ld hl,MapData+2
	ld (MAPptr),hl
	ld hl,Mapbuf1
	ld (MapbufPtr),hl
	ld hl,premap
	ld (PreMapPTR),hl

	call Updatemapfull



	ld hl, plotsscreen
	call safecopy


	IM 2
	ld a, %00000110
	out (4), a
	
	ei
		

	ld a, Balkstone
	ld (Player1 + Player_Unit), a
	ld a, 7
	ld (Player1 + Player_Direction), a
	ld (Player1 + Player_Current_Direction), a

	ld hl, 0*256+60
	ld (Player1 + Player_Map_X), hl
	ld hl, 0*256+40
	ld (Player1 + Player_Map_Y), hl

	SET Load_Ship_Sprite, (IY + flag + 1)


Loop:


	call updatemap

	ld a,(MapY)
	and tile_width*8-1
	ld h,a
	ld e,PMAPWIDTH*Tile_width
	call HxE

	ld de,mapbuf1
	add hl,de


	
	ld de, plotsscreen
	ld bc, 768
	ldir
_
	call Context_Sensitive_Get_Key
	call Draw_Ship
	BIT Ship_Mode, (IY + flag + 1)
	call nz, Move_Ship_At_Speed
	BIT MenuMode, (IY + flag)
	call nz, Draw_Menu

	BIT Cursor_Mode, (IY + flag)
	jr z, _
	call Draw_Select_Cursor
 	BIT Object_Ready_To_Add, (IY + flag)
 	call z, Check_Cursor_Over_Building


		
_


	ld hl, plotsscreen
	call safecopy


	jr Loop





Play_One_SAD_Frame:

	di

	EX AF, AF'
	EXX
	push ix
	push iy
	ld iy, flags
	ld a, (InterruptTimer)
	inc a
	cp 30
	jr nz, _
	
	xor a

_
	ld (InterruptTimer), a
	call Continue_Building_Construction
	call Play_Ship_Animation

	ld	a,%00001000
	out	(3),a
	ld	a,%00001010
	out	(3),a


	EX AF, AF'
	EXX
	pop iy
	pop ix

	ei
	RET


End_SAD_Frame:


















_
	ld (delta),hl

	bit 5,a
	jr nz,skipspeedon
	in a,(2)
	rla
	jr nc,skipspeedon
	ld a,1
	out ($20),a
	ld a,$FF
skipspeedon:
	bit 6,a
	jr nz,skipspeedoff
	in a,(2)
	rla
	jr nc,skipspeedoff
	xor a
	out ($20),a
	ld a,$FF
skipspeedoff:

	ld a,$FF
	out (1),a
	ld a,$FD
	out (1),a
	nop
	nop
	in a,(1)
	bit 6,a
	jp nz,loop
	ld hl,$9D95
	ld de, 12000
	bcall(_DelMem)
	bcall(_JForceCmdNoChar)


movemapdown:
	ld hl,(mapY)
	ld de,(delta)
	add hl,de
	push hl
		ld hl,(mapheight)
		ld de,PMAPHEIGHT-2
		or a
		sbc hl,de
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
	pop de
	call cphlde
	jr c,okaydown
	ex de,hl
okaydown:
	ld (mapY),hl
	ret


movemapup:
	ld hl,(mapY)
	ld de,(delta)
	or a
	sbc hl,de
	jr nc,notzeroup
	ld hl,0
notzeroup:
	ld (mapY),hl
	ret

	
movemapright:

	ld hl,(mapx)
	ld de,(delta)
	add hl,de
	push hl
		ld hl,(mapwidth)
		ld de,PMAPWIDTH
		or a
		sbc hl,de
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
		sla l
		rl h
	pop de
	ex de,hl
	call cphlde
	jr c,okayright
	ex de,hl
okayright:
	ld (mapx),hl
	ret


movemapleft:

	ld hl,(mapx)
	ld de,(delta)
	or a
	sbc hl,de
	jr nc,notzeroleft
	ld hl,0
notzeroleft:
	ld (mapx),hl
	ret
CpHLDE:
	or a
	sbc hl,de
	add hl,de
	ret
	
SafeCopy:	
	di
	ld c,$10
	ld a,$80
setrow:
	in f,(c)
	jp m,setrow
	out ($10),a
	ld de,12
	ld a,$20
col:
	in f,(c)
	jp m,col
	out ($10),a
	push af
	ld b,64
row:
	ld a,(hl)
rowwait:
	in f,(c)
	jp m,rowwait
	out ($11),a
	add hl,de
	djnz row
	pop af
	dec h
	dec h
	dec h
	inc hl
	inc a
	cp $2c
	jp nz,col
	ei
	ret

PMAPBUFSIZE = PMAPWIDTH*PMAPHEIGHT*8*Tile_width*Tile_width
PREMAPBUFSIZE = (PMAPWIDTH+1)*PMAPHEIGHT
PreMap = $9d95
Mapbuf1 = $9D95+2500
MapperRam = savesscreen+2

#include "Graymapper.inc"

Updatemapfull:
		
	call DrawMapFull
	ld a,(MapY)
	and tile_width*8-1
	ld h,a
	ld e,PMAPWIDTH*Tile_width
	call HxE

	ld de,mapbuf1
	add hl,de


	
	ld de, plotsscreen
	ld bc, 768
	ldir

	ret


Map_X = MapX
Map_Y = MapY